﻿using System;
using System.Collections.Generic;

namespace NLite.Collections.Internal
{

    [Serializable]
    class SyncList<T> : ICollection<T>
    {
        protected readonly object InnerLocker = new object();
        private List<T> InnerList;
        public SyncList()
        {
            InnerList = new List<T>();
        }
        public SyncList(int capacity)
        {
            InnerList = new List<T>(capacity);
        }
        public SyncList(IEnumerable<T> collection)
        {
            InnerList = new List<T>(collection);
        }

        public bool Contains(T item)
        {
            return InnerList.Contains(item);
        }

        public void Clear()
        {
            lock (InnerLocker)
                InnerList.Clear();
        }

        public void Add(T item)
        {
            lock (InnerLocker)
                InnerList.Add(item);
        }

        public bool Remove(T item)
        {
            if (InnerList.Contains(item))
                lock (InnerLocker)
                    return InnerList.Remove(item);
            return false;
        }

        public void CopyTo(T[] array, int arrayIndex)
        {
            InnerList.CopyTo(array, arrayIndex);
        }


        public int Count
        {
            get { return InnerList.Count; }
        }


        public IEnumerator<T> GetEnumerator()
        {
           return InnerList.GetEnumerator();
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

    }
}
